package com.mallbook.controller.servlet.admin;

import com.alibaba.fastjson.JSON;
import com.mallbook.model.exception.ServiceException;
import com.mallbook.model.pojo.Goods;
import com.mallbook.model.pojo.Orders;
import com.mallbook.model.pojo.OrdersDetails;
import com.mallbook.model.pojo.PageBean;
import com.mallbook.model.service.GoodsService;
import com.mallbook.model.service.OrderService;
import com.mallbook.model.utils.MallDefaultConfig;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-07-06
 */
@WebServlet("/admin/orderDetail")
public class OrderDetailServlet extends HttpServlet {
    private OrderService orderService = new OrderService();
    private GoodsService goodsService = new GoodsService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String opr = req.getParameter("opr");
        switch (opr) {
            case "select":
                doSelect(req, resp);
                break;
            case "selectOrder":
                doSelectOrder(req, resp);
                break;
            case "update":
                doUpdate(req, resp);
                break;
        }
    }

    /**
     * 修改订单信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("id");
        String upshr = req.getParameter("upshr");
        String upshdz = req.getParameter("upshdz");
        String uptel = req.getParameter("uptel");
        String upddje = req.getParameter("upddje");
        String state = req.getParameter("state");
        JSONObject jsonObject = new JSONObject();
        try {
            orderService.update(upshr, upshdz, uptel, upddje, Integer.parseInt(state), Integer.parseInt(oid));
            jsonObject.put("msg", "ok");
        } catch (ServiceException e) {
            e.printStackTrace();
            jsonObject.put("msg", "error");
        }
        PrintWriter writer = resp.getWriter();
        writer.print(jsonObject);
        writer.close();
    }

    private void doSelectOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        PrintWriter writer = resp.getWriter();
        Orders orders = null;
        try {
            orders = orderService.getOrdersById(Integer.parseInt(oid));
            String orderJson = JSON.toJSONString(orders);
            writer.print(orderJson);
            writer.close();
        } catch (ServiceException e) {
            e.printStackTrace();
        }
    }

    private void doSelect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        Orders orders = orderService.getOrdersById(Integer.parseInt(oid));
        req.setAttribute("order", orders);
        List<OrdersDetails> ordersDetailsList = orders.getOrdersDetails();
        List<Goods> goodsList = new ArrayList<>();
        for (OrdersDetails o : ordersDetailsList
        ) {
            Goods goods = goodsService.getGoodsById(o.getBookId());
            goods.setStock(o.getBookNums()); // 将stock代替数量
            goods.setSellingPrice(o.getPrice()); // SellingPrice代替Price
            goodsList.add(goods);
        }
        req.setAttribute("goodsList", goodsList);
        req.getRequestDispatcher("/WEB-INF/admin/order_details.jsp").forward(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
